﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AspZone.Commerce.IntegrationTests.TestData
{
    public class TestDataLoader
    {
        private TestDataContext testDataContext;

        public TestDataLoader()
        {
            this.testDataContext = new TestDataContext();
        }

        public void DeleteAllData()
        {
            this.testDataContext = new TestData.TestDataContext();
            this.testDataContext.ProductCategories.DeleteAllOnSubmit(this.testDataContext.ProductCategories);
            this.testDataContext.ProductImages.DeleteAllOnSubmit(this.testDataContext.ProductImages);
            this.testDataContext.ProductProperties.DeleteAllOnSubmit(this.testDataContext.ProductProperties);
            this.testDataContext.Products.DeleteAllOnSubmit(this.testDataContext.Products);
            this.testDataContext.Categories.DeleteAllOnSubmit(this.testDataContext.Categories);
            this.testDataContext.SubmitChanges();
        }

        public Product GetProduct(int id)
        {
            var db = new TestDataContext();
            var product = db.Products.SingleOrDefault(p => p.ProductId == id);
            return product;
        }

        public Category GetCategory(int id)
        {
            var db = new TestDataContext();
            var category = db.Categories.SingleOrDefault(c => c.CategoryId == id);
            return category;
        }

        public Category InsertTestCategory(string name)
        {
            var c = new TestData.Category { Name = name, SEOName = name };
            this.testDataContext.Categories.InsertOnSubmit(c);
            this.testDataContext.SubmitChanges();
            return c;
        }

        public Product InsertTestProduct(string name)
        {
            var p = new TestData.Product { Name = name, SEOName = name };
            this.testDataContext.Products.InsertOnSubmit(p);
            this.testDataContext.SubmitChanges();
            return p;
        }

        public ProductCategory InsertTestProductCategory(Product product, Category category)
        {
            var pc = new TestData.ProductCategory { Product = product, Category = category };
            this.testDataContext.ProductCategories.InsertOnSubmit(pc);
            this.testDataContext.SubmitChanges();
            return pc;
        }

        public ProductImage InsertTestProductImage(Product product, string path)
        {
            var image = new ProductImage { Product = product, FullsizePath = path, ThumbnailPath = path };
            this.testDataContext.ProductImages.InsertOnSubmit(image);
            this.testDataContext.SubmitChanges();
            return image;
        }

        public ProductProperty InsertTestProductProperty(Product product, string name, string value)
        {
            var property = new ProductProperty { Product = product, Name = name, Value = value };
            this.testDataContext.ProductProperties.InsertOnSubmit(property);
            this.testDataContext.SubmitChanges();
            return property;
        }
    }
}
